package com.wechat.mina;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.model.qr.QRParameter;
import com.foxinmy.weixin4j.model.qr.QRResult;
import com.foxinmy.weixin4j.mp.WeixinComponentProxy;
import com.foxinmy.weixin4j.mp.WeixinProxy;
import com.foxinmy.weixin4j.mp.api.ComponentApi;
import com.foxinmy.weixin4j.mp.api.TmplApi;
import com.foxinmy.weixin4j.mp.type.IndustryType;
import com.foxinmy.weixin4j.token.PerTicketManager;
import com.foxinmy.weixin4j.token.TokenManager;
import com.foxinmy.weixin4j.type.TicketType;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
import com.qiniu.common.Config;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.util.HttpRequest;
import com.util.SystemConstant;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.*;

/**
 * Created by Administrator on 2017-10-22.
 */
public class ComponentUtil {
    //public static final Log logger = LogFactory.getLog(ComponentUtil.class);
    public static String jscode2sessionUri = "https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code";
    public static String appid = "wx8a5bf19a3b8d5cd3";


    public static String getToken() {
        try {
            Map<String, String> map = new HashMap();
            map.put("grant_type", "client_credential");
            map.put("appid", "wx8a5bf19a3b8d5cd3");
            map.put("secret", "52c497db3697593faf66936fd7e3f337");

            String rt = HttpRequest.sendGet("https://api.weixin.qq.com/cgi-bin/token", map);

            JSONObject json = JSONObject.parseObject(rt);

            if (json.getString("access_token") != null || json.getString("access_token") != "") {
                return json.getString("access_token");
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static synchronized String getMinaQr(String postData, String key) throws WeixinException {
        try {
            key = DigestUtils.md5Hex(key) + ".jpg";
            String imgUrl = SystemConstant.DOMAIN_IMAGE + "/" + key;
            if (StringUtils.isNotBlank(HttpRequest.sendGet(imgUrl, "qhash/md5"))) {
                return imgUrl;
            }
            WeixinRequestExecutor weixinExecutor = new WeixinRequestExecutor();
            WeixinResponse response = weixinExecutor.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + getToken(), postData);
            Auth auth = Auth.create(Config.ACCESS_KEY, Config.SECRET_KEY);
            Configuration cfg = new Configuration(Region.region0());
            UploadManager uploadManager = new UploadManager(cfg);
            uploadManager.put(IOUtil.toByteArray(response.getBody()), key, auth.uploadToken("image", key));
            return imgUrl;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return null;
    }
}
